// pages/study-room/study-room.js
const utils=require('../../utils/util')
var app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        setInter:'',
        hour:0,
        minute:0,
        second:0,
        isStart:false
    },

    startSetInter: function(){
        var that = this;
        //将计时器赋值给setInter
        that.data.setInter = setInterval(
            function () {
                that.data.second =that.data.second+ 1;
                that.data.minute = that.data.minute;
                if(that.data.second==60){
                    that.data.second=0;
                    that.data.minute = that.data.minute+1;
                }
                if(that.data.minute==60){
                that.data.hour = that.data.hour+1;
                    that.data.minute=0;
                }
                that.setData({ hour: that.data.hour});
                that.setData({ minute: that.data.minute });
                that.setData({ second: that.data.second });
                that.setData({isStart:true})
            }
      , 1000);   
    },
    end:function(){
        wx.switchTab({
            url: '/pages/study/study',
          })
    },

    endSetInter: function(){
        var that = this;
        var time=utils.formatTime(new Date())
        clearInterval(that.data.setInter)
        console.log(that.data)
        wx.request({
          url: app.globalData.url+'/d',
          method:'POST',
          data:{
            second:that.data.second,
            minute:that.data.minute,
            hour:that.data.hour,
            time:time
          },
          header:{
              'content-type':'application/x-www-form-urlencoded'
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        if(this.data.isStart){
            this.endSetInter()
        wx.switchTab({
          url: '/pages/study/study',
        })
        console.log(1)
        wx.showToast({
            title: '学习中断，记录已保存',
            icon: 'none',
            duration: 1500
        })}
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        if(this.data.isStart){
            this.endSetInter()
            console.log(2)
            wx.showToast({
                title: '学习中断，记录已保存',
                icon: 'none',
                duration: 1500
            })}
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})